package dao;

// Generated 2 juin 2012 17:41:31 by Hibernate Tools 3.4.0.CR1

import static org.hibernate.criterion.Example.create;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.SessionFactory;

import beans.Client;
import beans.Passager;
import dao.interfaces.IDaoPassager;

/**
 * Home object for domain model class Passager.
 * 
 * @see beans.Passager
 * @author Hibernate Tools
 */
public class DaoPassager implements IDaoPassager {

	private static final Log log = LogFactory.getLog(DaoPassager.class);

	private final SessionFactory sessionFactory = getSessionFactory();

	protected SessionFactory getSessionFactory() {
		try {
			return HibernateUtil.getSessionFactory();
		} catch (Exception e) {
			log.error("Could not locate SessionFactory in JNDI", e);
			throw new IllegalStateException(
					"Could not locate SessionFactory in JNDI");
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see dao.IDaoPassager#persist(beans.Passager)
	 */
	@Override
	public void persist(Passager transientInstance) {
		log.debug("persisting Passager instance");
		try {
			sessionFactory.getCurrentSession().persist(transientInstance);
			log.debug("persist successful");
		} catch (RuntimeException re) {
			log.error("persist failed", re);
			throw re;
		}
	}

	public void attachDirty(Passager instance) {
		log.debug("attaching dirty Passager instance");
		try {
			sessionFactory.getCurrentSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Passager instance) {
		log.debug("attaching clean Passager instance");
		try {
			sessionFactory.getCurrentSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see dao.IDaoPassager#delete(beans.Passager)
	 */
	@Override
	public void delete(Passager persistentInstance) {
		log.debug("deleting Passager instance");
		try {
			sessionFactory.getCurrentSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see dao.IDaoPassager#merge(beans.Passager)
	 */
	@Override
	public Passager merge(Passager detachedInstance) {
		log.debug("merging Passager instance");
		try {
			Passager result = (Passager) sessionFactory.getCurrentSession()
					.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see dao.IDaoPassager#findById(int)
	 */
	@Override
	public Passager findById(int id) {
		log.debug("getting Passager instance with id: " + id);
		try {
			Passager instance = (Passager) sessionFactory.getCurrentSession()
					.get("beans.Passager", id);
			if (instance == null) {
				log.debug("get successful, no instance found");
			} else {
				log.debug("get successful, instance found");
			}
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<Passager> findByExample(Passager instance) {
		log.debug("finding Passager instance by example");
		try {
			List<Passager> results = (List<Passager>) sessionFactory
					.getCurrentSession().createCriteria("beans.Passager")
					.add(create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public ArrayList<Passager> getPassagerFromClient(Client client) {
		ArrayList<Passager> liste = new ArrayList<Passager>();

		Query q = HibernateUtil.getSessionFactory().getCurrentSession()
				.createQuery("from Passager");

		List<Passager> list = q.list();

		if (list != null) {
			for (Passager p : list) {
				if (p.getClient().getCliNum() == client.getCliNum()) {
					liste.add(p);
				}
			}
		}
		return liste;
	}
}
